import Taro from "@tarojs/taro";
import {
  GET_USERINFO,
  GET_OPENID,
  UPLOAD_USERINFO,
  CONFIRM_UPLOAD_USERINFO,
  CONFIRM_LOGIN
} from "../constants/user";
import { appid, appSecret, serverDomain, port } from "../config/config";

export function onGetUserInfo(userInfo) {
  return dispatch => {
    Taro.showToast({
      title: "登录中...",
      icon: "loading",
      mask: true
    })
    dispatch({
      type: GET_USERINFO,
      payload: userInfo
    });
  };
}

export function onLoginSession() {
  return async dispatch => {
    Taro.checkSession({
      success: function() {
        Taro.login({
          success: function(res) {
            if (res.code) {
              Taro.request({
                url: "https://api.weixin.qq.com/sns/jscode2session",
                method: "GET",
                data: {
                  appid: appid,
                  secret: appSecret,
                  js_code: res.code,
                  grant_type: "authorization_code"
                },
                success: function(data) {
                  dispatch({
                    type: GET_OPENID,
                    payload: data
                  });
                },timeout:10000,
                fail:function(){
                  Taro.atMessage({
                    message: "网络出错",
                    info: "error",
                    duration:10000
                  })
                },
              });
            }
          }
        });
      },
      fail: function() {
        Taro.login({
          success: function(res) {
            if (res.code) {
              Taro.request({
                url: "https://api.weixin.qq.com/sns/jscode2session",
                method: "GET",
                data: {
                  appid: appid,
                  secret: appSecret,
                  js_code: res.code,
                  grant_type: "authorization_code"
                },
                success: function(data) {
                  dispatch({
                    type: GET_OPENID,
                    payload: data
                  });
                }
              });
            }
          }
        });
      }
    });
  };
}

export function onUploadUserInfo(data,cb) {
  return dispatch => {
      Taro.request({
        url: `${serverDomain}:${port}/user/submitUserInfo`,
        method: "POST",
        data: data,
        success: function(res) {
          res.data.code === 1
            ? Taro.atMessage({
                message: "登录失败",
                info: "error",
                duration:3000
              })
            : (
              cb&&cb(),
              dispatch({
              type: UPLOAD_USERINFO,
              payload: res.data
            }));
        },
        complete: Taro.hideToast
      });
  };
}

export function onGetServerUserInfo(data,cb) {
  return dispatch => {
      Taro.request({
        url: `${serverDomain}:${port}/user/getUserInfo`,
        method: "POST",
        data: data,
        success: function(res) {
          res.data.code === 1
            ? Taro.atMessage({
                message: "登录失败",
                info: "error",
                duration:3000
              })
            : (
              cb&&cb(),
              dispatch({
              type: UPLOAD_USERINFO,
              payload: {code:res.data.code,msg:res.data.msg,data:res.data.data[0]}
            }));
        },
        timeout:5000,
        fail:function(){
          Taro.atMessage({
            message: "网络出错",
            info: "error",
            duration:10000
          })
        },
        complete: Taro.hideToast
      });
  };
}

export function onConfirmUploadUserInfo() {
  return {
    type: CONFIRM_UPLOAD_USERINFO
  };
}

export function onConfirmLogin() {
  return {
    type: CONFIRM_LOGIN
  };
}
